package com.nbers.springcloud.microservice.controller;

import com.nbers.springcloud.microservice.entity.Item;
import com.nbers.springcloud.microservice.entity.base.JsonResult;
import com.nbers.springcloud.microservice.service.ItemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.nbers.springcloud.microservice.controller.base.BaseController;

import java.util.List;
import java.util.Map;

/**
 * @auther 代码生成器
 * @create 2020-04-17
 * @describe 前端控制器
 */
@RestController
@RequestMapping("/item")
@Api("item服务接口")
@Slf4j
public class ItemController extends BaseController {

    @Autowired
    ItemService itemService;

    @ApiOperation(("获取全部item列表"))
    @GetMapping(value = "selectAll")
    public JsonResult<List<Item>> selectAll() {
        log.info("JSONIN=");
        JsonResult<List<Item>> jsonResult = new JsonResult<List<Item>>();
        List<Item> list = itemService.list();
        if (null != list) {
            jsonResult.setData(list);
        } else {
            jsonResult.setState(JsonResult.ERROR);
            jsonResult.setMessage("Data is null");
        }
        log.info("JSONOUT=" + jsonResult.toString());
        return jsonResult;
    }

    @ApiOperation("根据Id获取item")
    @GetMapping(value = "selectById")
    public JsonResult<Item> selectById(@RequestParam Map<String, Object> params) {
        log.info("JSONIN=" + params.toString());
        JsonResult<Item> jsonResult = new JsonResult<Item>();
        Item item = new Item();
        String id = (String) params.get("id");
        if (null != id && !"".equals(id)) {
            item = itemService.getById(id);
            if (null != item) {
                jsonResult.setData(item);
            } else {
                jsonResult.setState(JsonResult.ERROR);
                jsonResult.setMessage("Data is null");
            }
        }
        log.info("JSONOUT=" + jsonResult.toString());
        return jsonResult;
    }
}

